package com.android.baseconfig.common.utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lei
 * @description SafeText
 * @date 2018/5/5
 */
public final class SafeText {
    private SafeText() {

    }

    /**
     * Fortify处理
     *
     * @param str str
     * @return string
     * @see [类、类#方法、类#成员]
     */
    public static String safePath(String str) {
        Map<String, String> map = getBaseList();
        map.put(":", ":");
        map.put("_", "_");
        map.put(".", ".");
        map.put("/", "/");
        map.put("-", "-");
        map.put("(", "(");
        map.put(")", ")");
        map.put(",", ",");
        map.put("@", "@");
        map.put("?", "?");
        map.put("&", "&");
        map.put("=", "=");
        map.put(File.separator, File.separator);
        return filter(map, str);
    }

    /**
     * Fortify处理
     *
     * @param e e
     * @return str
     * @see [类、类#方法、类#成员]
     */
    public static String safeExceptionLog(Throwable e) {
        if (null == e) {
            return "";
        }

        if (e instanceof IOException) {
            return "Invalid file::" + e.getClass().getName();
        }

        StringBuffer error = new StringBuffer();
        error.append("Exception:" + e.getClass().getName() + "\n");
        for (StackTraceElement element : e.getStackTrace()) {
            error.append("\t\tat ").append(element.getClassName());
            error.append('.').append(element.getMethodName());
            error.append('(').append(element.getFileName());
            error.append(':').append(element.getLineNumber());
            error.append(')').append('\n');
        }

        if (null != e.getCause()) {
            error.append(safeExceptionLog(e.getCause()));
        }

        return error.toString();
    }

    /**
     * 获取基本白名单
     *
     * @return map
     * @see [类、类#方法、类#成员]
     */
    private static Map<String, String> getBaseList() {
        Map<String, String> map = new HashMap<String, String>();

        for (char lower = 'a'; lower <= 'z'; lower++) {
            map.put(String.valueOf(lower), String.valueOf(lower));
        }
        for (char upper = 'A'; upper <= 'Z'; upper++) {
            map.put(String.valueOf(upper), String.valueOf(upper));
        }
        for (int number = 0; number <= 9; number++) {
            map.put(String.valueOf(number), String.valueOf(number));
        }

        map.put(" ", " ");

        return map;
    }

    /**
     * 白名单过滤
     *
     * @param map map
     * @param str str
     * @return str
     * @see [类、类#方法、类#成员]
     */
    private static String filter(Map<String, String> map, String str) {
        if (null == str) {
            return "";
        }
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char key = str.charAt(i);
            String word = map.get(key + "");
            if (word != null) {
                temp.append(word);
            }
        }
        return temp.toString();
    }
}